package com.huaxin.hxmodulesystem.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huaxin.hxmodulesystem.dto.ResponseResult;
import com.huaxin.hxmodulesystem.dto.request.AddRoleReqDTO;
import com.huaxin.hxmodulesystem.dto.request.UpdateRoleReqDTO;
import com.huaxin.hxmodulesystem.models.Role;
import com.huaxin.hxmodulesystem.service.RoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.persistence.Index;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "角色")
@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/sys/role")
public class RoleController {
    private final RoleService roleService;

    /**
     * 添加供应商角色
     *
     * @param in 用户ID
     * @return 是否成功
     */
    @Operation(summary = "添加角色", description = "添加角色")
    @PostMapping("/addRole")
    public ResponseResult<String> addRole(@RequestBody AddRoleReqDTO in) {
        return roleService.addRole(in);
    }

    /**
     * 修改供应商角色
     *
     * @param in 用户ID
     * @return 是否成功
     */
    @Operation(summary = "修改供应商角色", description = "修改供应商角色")
    @PostMapping("/updateRole")
    public ResponseResult<String> updateRole(@RequestBody UpdateRoleReqDTO in) {
        return roleService.updateRole(in);
    }

    /**
     * 删除商角色
     *
     * @return 是否成功
     */
    @Operation(summary = "删除商角色", description = "删除商角色")
    @GetMapping("/deleteRole")
    public ResponseResult<String> deleteRole(@Parameter(description = "角色ID", name = "id", required = true) @RequestParam(name = "id", required = true) String id) {
        return roleService.deleteRole(id);
    }

    /**
     * 根据用户ID查询角色
     *
     * @param userId 用户ID
     * @return 供应商角色列表
     */
    @Operation(summary = "根据用户ID查询角色", description = "根据用户ID查询角色")
    @GetMapping("/queryRole")
    public ResponseResult<Role> queryRole(@Parameter(description = "用户ID", name = "userId", required = true) @RequestParam(name = "userId", required = true) String userId) {
        return ResponseResult.ok(roleService.queryRole(userId));
    }

    /**
     * 查询所有角色
     *
     * @return 角色列表
     */
    @Operation(summary = "查询所有角色", description = "查询所有角色")
    @GetMapping("/queryAllRole")
    public ResponseResult<Page<Role>> queryAllRole(@Parameter(description = "页码", name = "pageNum", required = true) Integer pageNum,
                                                   @Parameter(description = "页大小", name = "pageSize", required = true) @RequestParam(name = "pageSize", required = true) Integer pageSize,
                                                   @Parameter(description = "角色名称", name = "roleName", required = true) @RequestParam(name = "roleName", required = false) String roleName) {
        return roleService.queryAllRole(pageNum, pageSize, roleName);
    }

    /**
     * 给用户分配角色
     *
     * @param userId 用户ID
     * @return 是否成功
     */
    @Operation(summary = "给用户分配角色", description = "给用户分配角色")
    @GetMapping("/assignRole")
    public ResponseResult<String> assignRole(@Parameter(description = "用户ID", name = "userId", required = true) @RequestParam(name = "userId", required = true) String userId, @Parameter(description = "角色ID", name = "roleId", required = true) @RequestParam(name = "roleId", required = true) String roleId) {
        return roleService.assignRole(userId, roleId);
    }
}
